<template>
	<view style="padding: 20rpx;">
			<view style="margin-bottom: 110rpx;">
				
				<scroll-view :scroll-top="scrollTop" scroll-y="true"    style="height: 80vh;">
				<view v-for="item in addressList" :key="item.id" class="box" style="margin-bottom: 10rpx;">
					<view   @click="selectAddress(item)">
						<view style="margin-bottom: 10rpx;">{{ item.address }}</view>
						<view style="color: #888; margin-bottom: 10rpx;">
							<text>{{ item.userName }}</text>
							<text style="margin-left: 20rpx;">{{ item.phone }}</text>
						</view>
					</view>
					<!-- update and delete -->
					<view style="text-align: right;">
						<uni-icons type="compose" size="40" @click="handleEdit(item.id)"></uni-icons>
						<uni-icons type="trash" size="40" style="margin-left: 20rpx;" @click="del(item.id)"></uni-icons>
					</view>
				</view>
				</scroll-view>
				
			</view>
			
			<navigator url="/pages/editAddress/editAddress">
				<button type="primary" style="background-color: dodgerblue; border-color: dodgerblue;
					position: fixed; bottom: 10rpx; width: calc(100% - 40rpx)">新增收货地址</button>
			</navigator>
		</view>
</template>

<script>
	import '@/static/css/global.css'
	export default {
		data() {
			return {
				user: uni.getStorageSync('xm-user'),
				addressList: []
			}
		},
		onShow(option) {
			// console.log(option.userId)
					this.load()
				},
		methods: {
						selectAddress(addressItem) {
							addressItem.commennt = uni.getStorageSync('xm-orders').comment,
							uni.setStorageSync('xm-orders', addressItem),
							uni.navigateBack()
						},
						handleEdit(addressId) {
							uni.navigateTo({
								url: '/pages/editAddress/editAddress?addressId=' + addressId
							})
						},
						del(addressId) {
							this.$request.get('/address/deleteById/' + addressId).then(res => {
								if (res.code === '200') {
								  uni.showToast({
								    icon: "success",
								    title: '操作成功'
								  })
								  this.load()
								} else {
								  uni.showToast({
								    icon: "error",
								    title: res.msg
								  })
								}
							})
						},
						load(){
									this.$request.get('/address/selectAll/'+this.user.id).then(res => {
												this.addressList = res.data || []
											})
						}
				
	
		}
	}
</script>

<style>

</style>
